import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.AirPlane;
import model.Fly;
import model.Location;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;

public class FlyController extends Controller
{
	private static final long	serialVersionUID	= 8701635252341035856L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		super.doGet(request, response);
	}

	public void index(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		ServletContext sc = getServletContext();
		RequestDispatcher rd = sc.getRequestDispatcher("/main");
		rd.forward(request, response);
	}

	public void main(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException, IOException
	{

		// ArrayList<Employee> employees = (ArrayList<Employee>)
		// getSession().createCriteria(Employee.class).list();
		// Collections.sort(employees, new EmployeeSorterByWillFly());
		// for (Employee employee : employees){
		// Log.d(employee.getId() + " : " + employee.getWillFlyTimeOnThisMonth()
		// + " : "+ employee.getName());
		// }
		String html = getViewHtml("fly", "index");
		html = html.replaceAll("\\{fly\\_list\\}", getFlyList());
		renderView(html, response);
	}

	@SuppressWarnings("unchecked")
	public String getFlyList()
	{
		StringBuilder list = new StringBuilder();
		Criteria criteria = getSession().createCriteria(Fly.class);
		criteria.addOrder(Order.desc("date1"));
		ArrayList<Fly> flies = (ArrayList<Fly>) criteria.list();
		for (Fly fly : flies)
		{
			list.append(getStayListRow(fly, false));
		}
		return list.toString();
	}

	public StringBuilder getStayListRow(Fly fly, boolean ajax)
	{
		StringBuilder row = new StringBuilder();

		if ( !ajax )
			row.append("<tr id=\"text\" stay_row=\"" + fly.getId() + "\" class=\"stay-" + fly.getId() + "\">");
		row.append("<td>" + fly.getAirPlane().getMark() + "</td>");
		row.append("<td>" + fly.getDate1() + "</td>");
		row.append("<td>" + fly.getDate2() + "</td>");
		row.append("<td>" + fly.getDate3() + "</td>");
		row.append("<td>" + fly.getDate4() + "</td>");
		row.append("<td><img style=\"cursor: pointer\" src=\"" + StaticMethods.PATH + "images/edit.png\" id=\"edit_fly\" flyid=\"" + fly.getId() + "\"/></td>");
		row.append("<td><img style=\"cursor: pointer\" src=\"" + StaticMethods.PATH + "images/delete.png\" id=\"delete_fly\" flyid=\"" + fly.getId() + "\"/></td>");
		if ( !ajax )
			row.append("</tr>");
		return row;
	}

	public void dialogFly(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException, IOException
	{
		long flyid = Long.parseLong(getParameter("flyid") != null ? getParameter("flyid") : "-1");
		String dialog_html = getViewHtml("fly", "dialog");
		if ( flyid == -1 )
		{
			dialog_html = dialog_html.replaceAll("\\{airplane_list\\}", getAirplaneList(false, null));
			dialog_html = dialog_html.replaceAll("\\{location_src_list\\}", getLocationList(false, null));
			dialog_html = dialog_html.replaceAll("\\{location_via_list\\}", getLocationList(false, null));
			dialog_html = dialog_html.replaceAll("\\{location_dst_list\\}", getLocationList(false, null));
		}
		else
		{
			Fly fly = (Fly) getObjectByPk(Fly.class, flyid);
			dialog_html = dialog_html.replaceAll("\\{airplane_list\\}", getAirplaneList(true, fly));
			dialog_html = dialog_html.replaceAll("\\{location_src_list\\}", getLocationList(true, fly.getSrcPosition()));
			dialog_html = dialog_html.replaceAll("\\{location_via_list\\}", getLocationList(true, fly.getViaPosition()));
			dialog_html = dialog_html.replaceAll("\\{location_dst_list\\}", getLocationList(true, fly.getDstPosition()));
		}
		renderView(dialog_html, response);
	}

	@SuppressWarnings("unchecked")
	public String getAirplaneList(boolean flag, Fly fly)
	{
		StringBuilder list = new StringBuilder();
		ArrayList<AirPlane> airPlanes = (ArrayList<AirPlane>) getAllObjects(AirPlane.class);
		for (AirPlane airPlane : airPlanes)
		{
			if ( flag )
			{
				if ( airPlane.getId() == fly.getAirPlane().getId() )
				{
					list.append("<option selected=\"selected\" value=\"" + airPlane.getId() + "\">" + airPlane.getMark() + "</option>");
				}
				else
				{
					list.append("<option value=\"" + airPlane.getId() + "\">" + airPlane.getMark() + "</option>");
				}
			}
			else
			{
				list.append("<option value=\"" + airPlane.getId() + "\">" + airPlane.getMark() + "</option>");
			}
		}
		return list.toString();
	}

	@SuppressWarnings("unchecked")
	public String getLocationList(boolean flag, Location flocation)
	{
		StringBuilder list = new StringBuilder();
		ArrayList<Location> locations = (ArrayList<Location>) getAllObjects(Location.class);
		for (Location location : locations)
		{
			if ( flag )
			{
				if ( location.getId() == flocation.getId() )
				{
					list.append("<option selected=\"selected\" value=\"" + location.getId() + "\">" + location.getName() + "</option>");
				}
				else
				{
					list.append("<option value=\"" + location.getId() + "\">" + location.getName() + "</option>");
				}
			}
			else
			{
				list.append("<option value=\"" + location.getId() + "\">" + location.getName() + "</option>");
			}
		}
		return list.toString();
	}
}